<template>
    <div class="wrap">
        <div>
            <el-upload class="upload-demo"
                       ref="upload"
                       :multiple="false"
                       :drag="true"
                       :auto-upload="false"
                       :file-list="files"
                       :on-change="getFile"
                       :on-remove="removeFile"
                       :limit="fileLimitNum"
                       :on-exceed="handleExceed"
                       :before-upload="beforeImport"
                       @dragenter.native.stop.prevent
                       @dragover.native.stop.prevent
                       @drop.native.stop.prevent="handleFileDrop($event)">
                <div class="upload-demo-info">
                    <em class="el-icon-upload upload-icon"/>
                    <div class="el-upload__text">{{ $t('message.FastenerInfoImportDlg.dragHere') }}
                        <em>{{ $t('message.FastenerInfoImportDlg.clickUpload') }}</em>
                    </div>
                </div>
            </el-upload>
            <div v-if="errMsg !== ''" class="text-danger mt-2">{{ errMsg }}</div>
        </div>
        <div class="d-flex mt-2 align-items-center">
        </div>
        <div class="btn">
            <el-button type="primary" @click="importAssemblyXML" :disabled="importDisabled">
                {{ $t('message.FlightControlSimulationDesignTool.import') }}
            </el-button>
            <el-button size="small"
                       @click="cancel">{{ $t('message.FlightControlSimulationDesignTool.close') }}
            </el-button>

        </div>
    </div>
</template>
<script>

import DialogMixin from "../DialogMixin";
import {flightControlSimulationCommand} from "../../../command/FlightControlSimulationCommand";

export default {
    name: "FlightControlSimulationDesign",
    mixins: [DialogMixin],
    data() {
        return {
            isInit: false,
            files: [],
            errMsg: '',
            isExportSTL: true,
            isExportXML: true,
            importDisabled: true,
            fileLimitNum: 1,
            assembly: {},
            mateFeature: []
        }
    },
    mounted() {
        if (!this.isInit) {
            flightControlSimulationCommand.init(350, 350, 600, () => {
                flightControlSimulationCommand.clearAllHighLight();
                flightControlSimulationCommand.getAssemblyInfo((assembly, mateFeature) => {
                    this.assembly = {};
                    this.mateFeature = [];
                    this.assembly = assembly;
                    this.mateFeature = mateFeature;
                });
                this.isInit = true;
            })
        }
    },
    methods: {
        changePluginContainerHeight() {
            flightControlSimulationCommand.changePluginContainerHeight({
                minWidth: this.$el.clientWidth,
                maxHeight: this.$el.clientHeight + 10
            });
        },
        cancel() {
            flightControlSimulationCommand.clearAllHighLight();
            flightControlSimulationCommand.closeActiveCmd();
        },
        //文件超出数量限制的
        handleExceed(file, fileList) {
            flightControlSimulationCommand.eventBusAlertMsg('一次只能上传一个文件', 'warning');
        },
        //文件状态改变时需要调用：添加文件、上传成功/失败都会调用
        getFile(file, fileList) {
            let filetype = this.getFileType(file.name);
            if (filetype !== 'xml') {
                this.errMsg = this.$t('message.FlightControlSimulationDesignTool.importTypeError');
                this.importDisabled = true;
            } else {
                this.files.push(fileList[0]);
                this.errMsg = '';
                this.importDisabled = false;
            }
        },
        //获取上传文件的文件类型
        getFileType(fileName) {
            let delimiterIndex = fileName.lastIndexOf('.');
            return fileName.substring(delimiterIndex + 1).toLowerCase();
        },
        handleFileDrop(event) {
        },
        removeFile(file, fileList) {
            this.files = [];
            this.errMsg = '';
            this.importDisabled = true;
        },
        //上传前校验
        beforeImport(file) {
            //TODO 上传前的校验类型
            return true;
        },
        async importAssemblyXML() {
            flightControlSimulationCommand.readXmlFile(this.files[0]);
        },
    },
    watch: {},
    computed: {
        isUpload() {
            return true;
        }
    }
}
</script>

<style scoped>
.wrap {
    font-size: 12px;
    color: #606266
}

.limit {
    color: red;
}
</style>


